home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / pmode / exc_dx02 / exc_0.asm < prev    next >
Encoding:
Assembly Source File  |  1994-08-04  |  7.2 KB  |  206 lines

  1.  
  2. PAGE 60,132
  3. TITLE Exception handler for PharLap 386|DosX
  4. NAME  EXC_0
  5. .386
  6.  
  7.  
  8. ExcStruc struc
  9.      Number  dw  0
  10.      Code    dw  0
  11.      _DS     dw  0
  12.      _ES     dw  0
  13.      _FS     dw  0
  14.      _GS     dw  0
  15.      _SS     dw  0
  16.      _CS     dw  0
  17.      _EIP    dd  0
  18.      _EAX    dd  0
  19.      _EBX    dd  0
  20.      _ECX    dd  0
  21.      _EDX    dd  0
  22.      _ESI    dd  0
  23.      _EDI    dd  0
  24.      _EFlags dd  0
  25.      _ESP    dd  0
  26.      _EBP    dd  0
  27. ExcStruc ends
  28.  
  29.  
  30. ;****************************************************************
  31.  
  32. _DATA SEGMENT PUBLIC DWORD USE32 'DATA'
  33.  
  34.       excHookMain  dd        0          ; HLL hook to exceptions
  35.       except       ExcStruc  <>
  36.  
  37.   ;                dd 256 dup (?)       ; exception handler stack
  38.   ;   exc_TOS      label dword          ; stack start
  39.  
  40.       EXTRN  oldExcHandlers : dword     ; array[0..17] of far-pointers
  41.       PUBLIC except, excHookMain
  42. _DATA ENDS
  43.  
  44. _TEXT SEGMENT PUBLIC DWORD USE32 'CODE'
  45.       ASSUME CS:_TEXT, DS:_DATA
  46.  
  47.  
  48. comment ~
  49.  
  50.  A HLL interface for user installed exception handlers
  51.  
  52.  Stack layout:   ofs  38h   CR2       At time of orignal int
  53.                       34h   INT #     Int/Exception number
  54.                       30h   DosxFlag  For DosX umbrella handler
  55.                       2Ch   GS        At time of orignal int
  56.                       28h   FS        At time of orignal int
  57.                       24h   DS        At time of orignal int
  58.                       20h   ES        At time of orignal int
  59.                       1Ch   SS        At time of orignal int
  60.                       18h   ESP       At time of orignal int
  61.                       14h   EFLAGS    At time of orignal int
  62.                       10h   CS        At time of orignal int
  63.                       0Ch   EIP       At time of orignal int
  64.                       08h   EFLAGS    For DosX umbrella handler
  65.                       04h   CS        For DosX umbrella handler
  66.            SS:ESP->   00h   EIP       For DosX umbrella handler
  67.            SS:ESP->  -04h   Code      Exception code for exc 8,10-14
  68.  
  69.         ~
  70. _Rmode      equ 1                         ; int occurred in real mode
  71. _LDSregs    equ 2                         ; load seg regs on return
  72.  
  73. ;
  74. ; Stack layout refered to ss:[ebx]. EBX = entry ESP.
  75. ;
  76. ex_CR2      equ (dword ptr ss:[ebx+38h])  ; CR2 at time of interrupt
  77. ex_INUM     equ (dword ptr ss:[ebx+34h])  ; interrupt number
  78. ex_FL       equ (dword ptr ss:[ebx+30h])  ; flag bits, value 0-3
  79. ex_GS       equ (dword ptr ss:[ebx+2Ch])  ; GS at time of interrupt
  80. ex_FS       equ (dword ptr ss:[ebx+28h])  ; FS at time of interrupt
  81. ex_DS       equ (dword ptr ss:[ebx+24h])  ; DS at time of interrupt
  82. ex_ES       equ (dword ptr ss:[ebx+20h])  ; ES at time of interrupt
  83. ex_SS       equ (dword ptr ss:[ebx+1Ch])  ; SS:ESP at time of interrupt
  84. ex_ESP      equ (dword ptr ss:[ebx+18h])  ;
  85. ex_EFLAGS   equ (dword ptr ss:[ebx+14h])  ; EFLAGS at time of interrupt
  86. ex_CS       equ (dword ptr ss:[ebx+10h])  ; CS:EIP at time of interrupt
  87. ex_EIP      equ (dword ptr ss:[ebx+0Ch])  ;
  88. ex_DOSX_EFL equ (dword ptr ss:[ebx+08h])  ; return stack frame in
  89. ex_DOSX_CS  equ (dword ptr ss:[ebx+04h])  ; 386|DOS-Extender handler
  90. ex_DOSX_EIP equ (dword ptr ss:[ebx+00h])  ; return address
  91. ex_ICODE    equ (dword ptr ss:[ebx-04h])  ; exception error code
  92.  
  93.  
  94. ;---------------------------------------------------------------
  95.  
  96. align 4
  97. PUBLIC ExcGlue_0                       ; ISR for exception 0-7,9,15-17
  98. ExcGlue_0:
  99.         push eax                       ; save registers used
  100.         push ebx
  101.         mov ebx,esp
  102.         add ebx,8                      ; EBX -> DOSX_EIP
  103.         mov except.Code,0              ; no exception code
  104.         call ExcGlueMain               ; fill ExcStruc, patch return address
  105.         pop ebx
  106.         pop eax
  107.         iretd                          ; IRET and continue at ExcCont below
  108.  
  109. align 4
  110. PUBLIC ExcGlue_1                       ; ISR for exception 8, 10-14
  111. ExcGlue_1:
  112.         push eax                       ; save registers used
  113.         push ebx
  114.         mov ebx,esp
  115.         add ebx,0Ch                    ; EBX -> DOSX_EIP
  116.         mov eax,ex_ICODE               ; save exception code
  117.         mov except.Code,ax
  118.         call ExcGlueMain               ; fill ExcStruc, patch return address
  119.         pop ebx
  120.         pop eax
  121.         add esp,4                      ; discard error code
  122.         iretd                          ; IRET and continue at ExcCont
  123.  
  124. ;-------------------------------------------------------------------
  125. ;
  126. ;  DosX umbrella handler tranfers control to this point
  127. ;
  128.  
  129. ExcCont:cmp excHookMain,0              ; HLL hook installed ?
  130.         je ToDosx                      ; no, back to Dosx
  131.         sti
  132.         call [excHookMain]             ; yes, no HLL processing
  133.         mov esp,except._ESP            ; get old ESP, EBP unchanged
  134.         jmp [except._EIP]              ; return to EIP
  135.  
  136. ToDosx: push eax
  137.         push ecx
  138.         movzx eax,except.Number        ; get exception number
  139.         mov cl,al
  140.         imul eax,6                     ; far pointer is 6 bytes
  141.         lea eax,oldExcHandlers[eax]
  142.         push ds
  143.         mov edx,[eax]
  144.         mov ds,[eax+4]
  145.         mov eax,2533h                  ; set exception vector
  146.         int 21h
  147.         pop ds
  148.         mov eax,except._EIP            ; get EIP for exception
  149.         mov [esp+8],eax                ; put on stack
  150.         pop ecx
  151.         pop eax
  152.         ret                            ; return to cause of exception
  153.  
  154.  
  155. ;-------------------------------------------------------------------
  156.  
  157. ex_EBX   equ (dword ptr [esp+4])       ; EBX saved on stack
  158. ex_EAX   equ (dword ptr [esp+8])       ; EAX on stack
  159.  
  160. align 4
  161. ExcGlueMain:
  162.         mov eax,ex_INUM                ; load exception number
  163.         mov except.Number,ax           ; store it
  164.         mov except._ECX,ecx
  165.         mov except._EDX,edx
  166.         mov except._EDI,edi            ; EDI register
  167.         mov except._ESI,esi            ; ESI register
  168.         mov except._EBP,ebp            ; EBP register
  169.  
  170.         mov eax,ex_EAX
  171.         mov except._EAX,eax            ; EAX register
  172.         mov eax,ex_EBX
  173.         mov except._EBX,eax            ; EBX register
  174.         mov eax,ex_EIP
  175.         mov except._EIP,eax            ; EIP register
  176.         mov eax,ex_CS
  177.         mov except._CS,ax              ; CS register
  178.         mov eax,ex_EFLAGS
  179.         and eax,not 10000h             ; clear resume flag
  180.         mov except._EFlags,eax         ; EFlag register
  181.         mov eax,ex_ESP
  182.         mov except._ESP,eax            ; ESP register
  183.         mov eax,ex_SS
  184.         mov except._SS,ax              ; SS register
  185.         mov eax,ex_ES
  186.         mov except._ES,ax              ; ES register
  187.         mov eax,ex_DS
  188.         mov except._DS,ax              ; DS register
  189.         mov eax,ex_FS
  190.         mov except._FS,ax              ; FS register
  191.         mov eax,ex_GS
  192.         mov except._GS,ax              ; GS register
  193.  
  194.         mov eax,offset cs:ExcCont
  195.         mov ex_EIP,eax                 ; patch EIP resume location
  196.         and ex_FL,not _LDSregs         ; don't reload segment registers
  197.         mov ax,cs
  198.         add ax,8
  199.         mov ds,ax                      ; reload DS/ES incase <> SS_DATA
  200.         mov es,ax
  201.         ret
  202.  
  203. _TEXT ENDS
  204. END
  205.  
  206.